<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('品质异常单列表')"/>
    <link th:href="@{/static/css/plugins/cropper/cropper.min.css}" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="abnormalForm" th:action="@{/abnormalQuality/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3" shiro:hasPermission="abnormal:vendorCode">
                            <label>责任单位:</label>
                            <input type="text" name="vendorCode" autocomplete="off"/>
                        </li>

                        <li class="col-sm-3">
                            <label>供应商名称:</label>
                            <input type="text" name="vendorName" autocomplete="off"/>
                        </li>

                        <li class="col-sm-3">
                            <label>物料:</label>
                            <input type="text" name="materialCode" autocomplete="off"/>
                        </li>

                        <li class="col-sm-3">
                            <label>物料描述:</label>
                            <input type="text" name="materialDesc" autocomplete="off"/>
                        </li>

                        <li class="col-sm-3">
                            <label>发文时间(起):</label>
                            <input type="text" name="startDate" id="startDate" autocomplete="off"/>
                        </li>

                        <li class="col-sm-3">
                            <label>发文时间(止):</label>
                            <input type="text" name="endDate" id="endDate" autocomplete="off"/>
                        </li>

                        <li class="col-sm-3">
                            <label>状态：</label>
                            <select id="auditResult" name="auditResult">
                                <option></option>
                                <option value="2">通过</option>
                                <option value="1">审核中</option>
                                <option value="0">待提交</option>
                                <option value="-1">驳回</option>
                            </select>
                        </li>

                        <li class="col-sm-3">
                            <button id="abnormalSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i
                                    class="fa fa-search"></i>&nbsp;搜索
                            </button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置
                            </button>

                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="abnormalToolBar" role="group">
                <a class="btn btn-outline btn-success btn-rounded" onclick="addAbnormal()"
                   shiro:hasPermission="abnormal:add">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="operationRecord()">
                    <i class="fa fa-edit"></i> 操作记录
                </a>
            </div>
            <table id="abnormal" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>

<div style="display: none" id="myModal">
    <table class="layui-table" id="recordTable">
        <thead>
        <tr>
            <th>操作人</th>
            <th>时间</th>
            <th>状态</th>
            <th>备注</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

<!--shiro不能在js中生效-->
<div id="permissionDiv" style="display: none;">
    <a id="edit" class="btn btn-outline btn-info btn-rounded" shiro:hasPermission="abnormal:edit">编辑</a>
    <a id="audit" class="btn btn-outline btn-info btn-rounded" shiro:hasPermission="abnormal:audit">审核</a>
</div>

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<!-- Chosen -->
<script th:src="@{/static/js/plugins/chosen/chosen.jquery.js}"></script>

<script type="text/javascript">

    $(function () {
        // 初始化时间插件
        laydate.render({elem: '#startDate'});
        laydate.render({elem: '#endDate'});
    });

    var searcherBS = Searcher.createBS('#abnormal',
        [
            {radio: true},
            {title: '责任单位', field: 'vendorCode'},
            {title: '供应商名称', field: 'vendorName'},
            {title: '物料', field: 'materialCode'},
            {title: '物料描述', field: 'materialDesc'},
            {title: '发文者', field: 'submit'},
            {title: '发文日期', field: 'submitDate'},
            {
                title: '状态', field: 'auditResult',
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return "待提交";
                    } else if (value == 1) {
                        return "审核中";
                    } else if (value == 2) {
                        return "审核通过";
                    } else if (value == -1) {
                        return "审核驳回";
                    }
                }
            },
            {title: '审核备注', field: 'auditComment'},
            {
                title: "操作", field: '',
                formatter: function (value, row, index) {
                    let val = '';
                    if ($("#edit").length && (row.auditResult == 0 || row.auditResult == -1)) {
                        $("#edit").attr("onclick", "edit('" + row.pkId + "')");
                        val += $("#edit").prop("outerHTML");
                    }
                    if (row.auditResult == 1 && $("#audit").length) {
                        $("#audit").attr("onclick", "audit('" + row.pkId + "')");
                        val += $("#audit").prop("outerHTML");
                    }
                    return '<a class="btn btn-outline btn-success btn-rounded" onclick="func(\'' + row.pkId + '\')">查看</a>' + val;
                }
            }
        ]
    );

    /**
     * 查询数据
     */
    function refresh() {
        searcherBS.refresh();
    }

    /**
     * 关闭模态框
     */
    function closeDialog() {
        layer.closeAll();
    }

    /**
     * 跳转至新增页
     */
    function addAbnormal() {
        var index = Dialog.openUrl("新增", ctxPath + '/abnormalQuality/addAbnormal', {
            area: ['900px', '700px'],
            maxmin: true,
            end: function () {
            }
        });
        layer.full(index);
    }

    /**
     * 跳转至详情页
     */
    function func(pkId) {
        var index = Dialog.openUrl("详情", ctxPath + '/abnormalQuality/details?pkId=' + pkId, {
            area: ['800px', '1000px'],
            maxmin: true,
            end: function () {
            }
        });
        layer.full(index);
    }

    /**
     * 跳转至编辑页
     */
    function edit(pkId) {
        var index = Dialog.openUrl("编辑", ctxPath + '/abnormalQuality/vendorEdit?pkId=' + pkId, {
            area: ['800px', '800px'],
            maxmin: true
        });
        layer.full(index);
    }

    /**
     * 跳转至审核页
     */
    function audit(pkId) {
        var index = Dialog.openUrl("审核", ctxPath + '/abnormalQuality/sqeAudit?pkId=' + pkId, {
            area: ['800px', '800px'],
            maxmin: true
        });
        layer.full(index);
    }

    function operationRecord() {
        var pkId = searcherBS.getSelectedRows("pkId");

        if (pkId.length !== 1) {
            Dialog.errorMsg("请选择一行进行操作");
            return;
        }

        $.ajax({
            type: "GET",
            url: ctxPath + '/abnormalQuality/record/' + pkId,
            contentType: "application/json;charsetset=UTF-8",//必须
            dataType: "json",//将JSONObj作为请求参数传递 ;
            cache: true,
            async: false,
            error: function (request) {
                showTipAlert("Connection error");
            },
            success: function (res) {
                var data = eval(res.result);
                var trCode = "";
                for (var item in data) {
                    trCode += "<tr>";
                    trCode += "<td >" + data[item].creator + "</td>";
                    trCode += "<td  >" + data[item].createTime + "</td>";
                    var val = '';
                    if (data[item].auditResult == 0) {
                        val = "发起";
                    } else if (data[item].auditResult == 1) {
                        val = "供应商提交";
                    } else if (data[item].auditResult == 2) {
                        val = "审核通过";
                    } else if (data[item].auditResult == -1) {
                        val = "审核驳回";
                    }
                    trCode += "<td >" + val + "</td>";
                    trCode += "<td id='auditCommentId'>" + (data[item].auditComment?data[item].auditComment:'') + "</td>";
                    trCode += "<tr>";
                }

                $("#recordTable tbody").html("");
                $("#recordTable tbody").append(trCode);
            }
        });

        Dialog.open("审核操作", $("#myModal"), {area: ['800px', '600px']});
    }
</script>
</body>
</html>
